<template>
  <div class="app-container">
    <h5 class="titleInfo">用户基础信息</h5>
    <el-form ref="form"  :model="params" :rules="rules" label-width="130px">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$route.query.type=='3'?'手机号':'会员手机号'" prop="name">
            <span>{{ params.username }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$route.query.type=='3'?'名称':'会员名称'" prop="name">
            <span>{{ params.realname }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$route.query.type=='3'?'昵称':'会员昵称'" prop="name">
            <span>{{ params.nickName || params.nickname }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="积分类型" prop="integralType">
            <el-select v-model="params.integralType">
              <el-option
                v-for="item in AccountEntry"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
          <el-form-item label="原积分数量" prop="name">
            <span>XX</span>
          </el-form-item>
        </el-col>-->
        <el-col :span="24">
          <el-form-item label="数量" prop="number">
            <el-input v-model="params.number" placeholder="请输入增加/减少的数量" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注信息" prop="name">
            <el-input v-model="params.remark" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button style="margin:30px 0" type="primary" @click="subimet('form')">立即提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import vipmange from "@/api/vipmange/vipmange";
import { detail, updateAccountIntegral } from "@/api/ServiceManagement";

const numberReg = (rule, value, callback) => {
  if (!value) {
    return callback(new Error("请输入增加/减少的数量"));
  } else {
    var reg = /^(0|[1-9][0-9]*|-[1-9][0-9]*)$/;
    if (reg.test(value)) {
      callback();
    } else {
      return callback(new Error("请录入正确的数值格式"));
    }
  }
};
export default {
  name: "remainder",
  data() {
    return {
      params: {
        userId: 0,
        integralType: "",
        number: 0,
        remark: ""
      },
      rules: {
        integralType: [
          { required: true, message: "请选择积分类型", trigger: "blur" }
        ],
        number: [{ required: true, validator: numberReg, trigger: "blur" }]
      },
      // 积分类型下拉框 3 ：提现积分 1:活动积分
      AccountEntry: [{ id: 2, name: "消费积分" }, { id: 3, name: "提现积分" }]
    };
  },
  inject: ["reload"],
  created() {
    // console.log(this.$route.query);
    this.userId = this.$route.query.userId;
    this.username = this.$route.query.username;
    this.detail();
  },
  methods: {
    // 详情
    async detail() {
      let res;
      if (this.$route.query.type) {
        res = await detail({ phone: this.username });
        console.log(111, res.data);
      } else {
        this.AccountEntry = [
          { id: 1, name: "活动积分" },
          { id: 3, name: "提现额度" },
          { id: 2, name: "消费积分" }
        ];
        res = await vipmange.detail2({ userId: this.userId });
      }
      if (res.data.code == 0) {
        res.data.data.integralType = 2;
        this.params = res.data.data;
      } else {
        this.$message({
          message: res.data.msg,
          type: "warning"
        });
      }
      // console.log(res.data);
    },
    // 提交
    subimet() {
      this.$refs["form"].validate(async valid => {
        if (valid) {
          // console.log(this.params)
          // this.params.number -= 0;
          const dataInfo = {
            userId: this.params.userId,
            integralType: this.params.integralType,
            number: Number(this.params.number),
            remark: this.params.remark
          };
          let res;
          if (this.$route.query.type) {
            res = await updateAccountIntegral(dataInfo);
          } else {
            res = await vipmange.updateAccountIntegral(dataInfo);
          }
          this.$message({
            message: res.data.msg,
            type: "success"
          });
          if (res.data.code == 0) {
            this.$router.go(-1);
          }
        }
      });
    }
  }
};
</script>
